//div+div>p>span+em^bq
//div>(content>ul>li.item$*5)+footer>p
//div#header>div.title.title1>p[title="hello world"]>a{Click me}
//div>(content>ul>li.item$)*5+footer>p
var aSingleTag =  ["br", "hr", "img", "input", "param", "meta", "link"];
var __id = 0;
var oGroup = {

}
// var strZenHtml = peel("(div#media_list_cell_$.media_list_cell_$.media_list_cell>div#media_name_$.media_name_$.media_name+img#delete_$.delete_$.delete[src='img/delete.png'])*10");
// console.log(strZenHtml);
/**
 * 剥离拆分
 * @param {zen表达式} strZen 
 */
function peel(strZen){
    var aMatch = strZen.match(/\((.*)\)/);
    if(aMatch&& aMatch[1]){
        oGroup["zengroup"+__id] = aMatch[1];
        strZen = strZen.replace(/\(.*\)/,"zengroup"+__id++);
    }
    var arrLevel = strZen.split(">"), strHTML = "";
    strHTML = getLevelHTML(arrLevel,0);
   return strHTML;
}
/**
 * 获取层级内容
 * @param {*} i 层级索引 
 */
function getLevelHTML(arr,i){
    if(i == arr.length){
        return;
    }
    
    var arrSiblingsInfo = getSiblingsHTML(arr[i]);
    var strClass = "";
    for(var j =0; j < arrSiblingsInfo[2].length; j++){
        strClass += " "+arrSiblingsInfo[2][j];
    }
    strClass = strClass.substr(1);
    var strAtrributes = arrSiblingsInfo[3];
    var strId = arrSiblingsInfo[4];
    var strContent = arrSiblingsInfo[5];
    var iTimes = arrSiblingsInfo[6];
    if(oGroup[arrSiblingsInfo[0]]){
        return iTimes ? repeatStr(peel(oGroup[arrSiblingsInfo[0]]),iTimes) : peel(oGroup[arrSiblingsInfo[0]]);
    }
    var bSingle = isSingleTag(arrSiblingsInfo[0]);
    var strSelf = "<"+ arrSiblingsInfo[0] + (strId ? " id=\""+strId + "\"" : "") + (strAtrributes ? " "+ strAtrributes +" " : "") + (strClass ? " class=\"" + strClass + "\"" : "") + (bSingle ? "/>" : ">" + (strContent ? strContent : "") + (getLevelHTML(arr,i+1) || "") + "</" + arrSiblingsInfo[0] + ">");
    return (arrSiblingsInfo[1]||"") + (iTimes ? repeatStr(strSelf,iTimes) : strSelf);
    
}
/**
 * 获取兄弟节点内容
 * @param {*} strTag 
 */
function getSiblingsHTML(strTag){
    var arrTags = strTag.split("+"),strSiblingsHTML="";
    
    if(arrTags.length > 1){
       
        for(var i =0; i < arrTags.length-1; i++){
           var aS = arrTags[i].split("*");
           var str = "";
           if(oGroup[aS[0]]){
                str += peel(oGroup[aS[0]]);
                if(!isNaN(aS[1])){
                    str = repeatStr(str, aS[1]);
                }
           }else{
                str = getLevelHTML([arrTags[i]],0);
                if(!isNaN(aS[1])){
                    str = repeatStr(str, aS[1]);
                }
           }
           strSiblingsHTML += str;
        }
    }
    var strLastTag = arrTags[arrTags.length -1];
    var arr = strLastTag.split("*");
    strLastTag = arr[0];
    var iTimes = arr[1];

    
    var strAtrributes;
    //获取属性
    strLastTag = strLastTag.replace(/\[.+\]/g,function(str){
        str = str.replace(/\]\[/," ");
        str = str.replace(/\[|\]/g,"");
        strAtrributes =  str;
        return "";
    });

    var arrClass = strLastTag.split(".");

    //获取id
    var arrId = arrClass[0].split("#"); 
    var strId = arrId[1] || "";
    arrClass[0] = arrId[0];

    //获取内容
    var strContent;
    arrClass[0] = arrClass[0].replace(/\{(.*)\}/,function(_,str){
        strContent = str;
        return "";
    });


    //获取额外属性
    strAtrributes = strAtrributes || "";
    strAtrributes += getExtraAttr(arrClass[0],strAtrributes);
    return [arrClass[0],strSiblingsHTML,arrClass&&arrClass.shift()&&arrClass||[],strAtrributes||"",strId,strContent, iTimes]
}
/**
 * 重复字符串
 * @param {字符串} str 
 * @param {重复次数} iTimes 
 */
function repeatStr(str,iTimes){
    var strTemp = "";
    for(var i = 0; i < iTimes; i++){
        strTemp += str.replace(/\$/g,(i+1)+"");
    }
    return strTemp;
}
/**
 * 是否单标签
 * @param {标签} val 
 */
function isSingleTag(val){
    return aSingleTag.indexOf(val) != -1;
}
/**
 * 获取格外属性
 * @param {标签} strTag 
 * @param {属性} strAtrributes 
 */
function getExtraAttr(strTag,strAtrributes){
    strAtrributes = strAtrributes || "";
    var str = "";
    switch(strTag){
        case "img":
            if(-1 == strAtrributes.indexOf("src")){
                str += " src=\"\"";
            }
            if(-1 == strAtrributes.indexOf("alt")){
                str += " alt=\"\"";
            }
            break;
        case "a":
            if(-1 == strAtrributes.indexOf("href")){
                str += " href=\"\"";
            }
            if(-1 == strAtrributes.indexOf("title")){
                str += " title=\"\"";
            }
            break;
    }
    return str;
}
module.exports.peel = peel;